<template>
  <div id="user-add">
    <h1>添加用户</h1>
    <el-form ref="userForm" :model="userForm" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="userForm.studentId" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="userForm.permission" placeholder="请选择用户类型">
          <el-option label="普通管理员" value="普通管理员"></el-option>
          <el-option label="超级管理员" value="超级管理员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item size="small">
        <el-button type="primary" @click="submitUser('userForm')">添加用户</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addUser } from '../api/api.js';
export default {
  name: 'user',
  data() {
    return {
      userForm: {
        username: '',
        password: '',
        studentId: '',
        permission: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        studentId: [
          { required: true, message: '学号不能为空', trigger: 'blur' },
          { min: 9, message: '学号至少需要9位', trigger: 'blur' }
        ],
        permission: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    submitUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = new URLSearchParams();
          params.append('username', this.userForm.username);
          params.append('password', this.userForm.password);
          params.append('studentId', this.userForm.studentId);
          params.append('permission', this.userForm.permission);

          addUser(params)
            .then(function (response) {
              if (response.data === '添加成功') {
                this.$message.success(response.data)
              } else if (response.data === '添加失败') {
                this.$message.info(response.data);
              }
            }.bind(this)) // 使用 bind 来绑定 this 到当前组件实例
            .catch(function (error) {
              console.log(error);
            });
        } else {
          ("表单验证失败，请检查输入是否正确");
          return false;
        }
      });
    },
  }
};
</script>

<style scoped>
#user-add {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

h1 {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}

el-form {
  width: 100%;
}

el-form-item {
  margin-bottom: 20px;
}

el-input,
el-select {
  width: 100%;
  box-sizing: border-box;
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 3px;
}

el-button {
  margin-right: 10px;
  padding: 8px 16px;
  background-color: #409eff;
  color: #fff;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}
</style>

<style scoped>
#add-user {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
  border-radius: 5px;
}

h1 {
  text-align: center;
  color: #333;
}

el-form {
  width: 100%;
  margin-top: 20px;
}

el-form-item {
  margin-bottom: 20px;
}

el-input,
el-select {
  width: 100%;
}

el-button {
  width: 100%;
}
</style>